﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChangePassword : System.Web.UI.Page
{
    public string messagetype = "";
    public string message = "";
    public ProxyWrapper.UserProxy.UserDetails UserObject { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                UserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
                if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString().Equals("cp"))
                {
                    string password = Request.Form["oldpassword"].ToString();
                    string newpassword = Request.Form["newpassword1"].ToString();
                    string renewpassword = Request.Form["newpassword2"].ToString();
                    if (!string.IsNullOrWhiteSpace(password) && !string.IsNullOrWhiteSpace(newpassword) && !string.IsNullOrWhiteSpace(renewpassword))
                    {
                        if (!newpassword.Equals(renewpassword))
                        {
                            messagetype = "E";
                            message = "New password do not matches";
                        } else if(!password.Equals(UserObject.Password))
                        {
                            messagetype = "E";
                            message = "Current password is not valid";
                        }
                        else
                        {
                            ProxyWrapper.UserServiceWrapper _wrap = new ProxyWrapper.UserServiceWrapper();
                            _wrap.ChangePassword(UserObject.UserId, newpassword, UserObject.HashCode);
                            messagetype = "S";
                            message = "Your password has been changed.";
                        }
                    }
                    else
                    {
                        messagetype = "E";
                        message = "Password(s) required";
                    }
                }
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }
}